package cn.jinjimi.app.custom.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import cn.jinjimi.app.R;
import cn.jinjimi.app.custom.entity.Custom;
import cn.jinjimi.app.main.adapter.extend.MyBaseAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by sun on 2015/5/14.
 */
public class CustomInputAdapter extends MyBaseAdapter<Custom> {

    private Context mContext = null;
    private List<Custom> selecteableCustoms = new ArrayList<Custom>();
    private List<Custom> selectedCustoms = new ArrayList<Custom>();

    public CustomInputAdapter(Context context) {
        this.mContext = context;
    }

    @Override
    public void setItems(List<Custom> items) {
        super.setItems(items);
        for (Custom c : getItems()) {
            if (!c.isInput)
                selecteableCustoms.add(c);
        }
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final InputHolder holder;
        final Custom item = getItem(position);
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.item_input_custom, null);
            holder = new InputHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (InputHolder) convertView.getTag();
        }
        holder.name.setText(item.name);
        holder.checkBox.setChecked(item.isSelect);

        holder.name.setTextColor(item.isInput ? Color.GRAY : Color.BLACK);
        holder.checkBox.setEnabled(!item.isInput);

        return convertView;
    }

    public class InputHolder {
        public TextView indexText;
        public TextView name;
        public CheckBox checkBox;

        public InputHolder(View view) {
            indexText = (TextView) view.findViewById(R.id.index);
            name = (TextView) view.findViewById(R.id.name);
            checkBox = (CheckBox) view.findViewById(R.id.item_cb);
        }
    }

    /* 点击任意一条Custom */
    public void onItemClick(int position, View view) {
        if(position < 0)
            return;
        Custom custom = getItem(position);
        if (custom.isInput) // 已导入，不作处理
            return;

        InputHolder holder = (InputHolder) view.getTag();
        holder.checkBox.toggle();// 在每次获取点击的item时改变checkbox的状态
        custom.isSelect = !custom.isSelect;

        if (custom.isSelect) {
            selectedCustoms.add(custom);
        } else {
            selectedCustoms.remove(custom);
        }
    }

    /* 全选或全不选 */
    public void setAllSelect(boolean check) {
        for (Custom c : getItems()) {
            if (!c.isInput)
                c.isSelect = check;
        }
        notifyDataSetChanged();

        selectedCustoms.clear();
        if (check)
            selectedCustoms.addAll(selecteableCustoms);
    }

    /* 获取所有被选中的用户 */
    public List<Custom> getSelectedCustoms() {
        return selectedCustoms;
    }

    /* 获取所有可被选中的用户 */
    public List<Custom> getSelecteableCustoms() {
        return selecteableCustoms;
    }

    /* 导入到服务器成功，刷新列表
     * 后来发现不需要刷新直接关闭，那就不需要更改UI啦
      * */
//    public void updateInputItem() {
//        for (Custom c : getSelectedCustoms()) {
//            c.setInput(true);
//            c.isSelect = false;
//        }
//        notifyDataSetChanged();
//
//        selectedCustoms.clear();
//        selecteableCustoms.clear();
//    }
}